#!/usr/bin/env python 2.6
#=============================================================================
'''
    Surveyor Unit Tests
'''
#=============================================================================
# Copyright 2011, Matt Peloquin. This file is part of Code Surveyor, 
# covered under GNU GPL v3 and is distributed WITHOUT ANY WARRANTY
#=============================================================================
import unittest
import sys
import os

import test_syspathfix
from framework import utils
from framework import filetype

def suite():
    return unittest.makeSuite(UnitTests, 'test')

class UnitTests(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print "================================================="
        print "            Starting unit Tests                  "
        print "================================================="

    # Path for the output files
    outPath = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'output'))

    def setUp(self):
        utils.init_surveyor_dir(sys.argv[0])

    def test_Dirs(self):
        print utils.surveyor_dir()
        print utils.runtime_dir()

    def test_PathParser(self):
        surveyorPathParser = utils.SurveyorPathParser(utils.surveyor_dir())
        print surveyorPathParser.dirList

    def test_StringDisplay(self):
        print utils.fit_string(utils.surveyor_dir(), 10)
        annoyChr = "Annoying ===>\x00\x01\x02\a<=== "
        print annoyChr, utils.strip_annoying_chars(annoyChr)

    def test_NonCodeFile(self):
        FILE_NAME = os.path.join(self.outPath, 'NonCodeTest')
        FILE_EXTS = [".c", ".jpg", ".exe", ".foo"]
        for ext in FILE_EXTS:
            try:
                os.remove(FILE_NAME + ext)
            except:
                pass
        for ext in FILE_EXTS:
            testFile = open(FILE_NAME + ext, "wb")
            testFile.write("\x7ELF - Non Code Test String\n")
            testFile.close()
        for ext in FILE_EXTS:
            fileName = FILE_NAME + ext
            if ext in [".exe", ".jpg"]:
                self.assertTrue( filetype.is_noncode_ext(fileName) )
            else:
                self.assertFalse( filetype.is_noncode_ext(fileName) )
            testFile = open(fileName, "rb")
            self.assertTrue( filetype.is_noncode_file(testFile) )
            testFile.close()

    def test_BinaryFile(self):
        FILE_NAME1 = os.path.join(self.outPath, 'BinaryFileTest1')
        FILE_NAME2 = os.path.join(self.outPath, 'BinaryFileTest1')
        try:
            os.remove(FILE_NAME1)
            os.remove(FILE_NAME2)
        except:
            pass
        testFile = open(FILE_NAME1, "w+")
        testFile.write("Test")
        self.assertTrue( filetype.is_text_file(testFile) )
        testFile.write("File is a bit longer")
        self.assertTrue( filetype.is_text_file(testFile) )
        testFile.close()

        testFile = open(FILE_NAME1, "a")
        testFile.write("add some <>{}{[] chars\n")
        testFile.close()
        testFile = open(FILE_NAME1, "rb")
        self.assertTrue( filetype.is_text_file(testFile) )
        testFile.close()

        testFile = open(FILE_NAME2, "wb+")
        testFile.write("".join([chr(x+110) for x in range(128)]))
        self.assertFalse( filetype.is_text_file(testFile) )
        testFile.close()

    def tearDown(self):
        pass


if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(unittest.TestSuite(suite()))

